package com.cdm.forum.controller;


import com.cdm.forum.common.Result;
import com.cdm.forum.common.ResultCode;
import com.cdm.forum.config.AppConfig;
import com.cdm.forum.exception.ApplicationException;
import com.cdm.forum.model.Message;
import com.cdm.forum.model.User;
import com.cdm.forum.service.IMessageService;
import com.cdm.forum.service.IUserService;
import com.cdm.forum.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "信息接口")
@RestController
@RequestMapping("/message")
@Slf4j
public class MessageController {

    @Resource
    private IMessageService messageService;
    @Autowired
    private IUserService userService;

    @ApiOperation("发送消息")
    @PostMapping("/send")
    public Result send(HttpServletRequest request,
                       @ApiParam("接收者") @RequestParam("receiveUserId") @NonNull Long receiveUserId,
                       @ApiParam("内容") @RequestParam("content") @NonNull String content) {

        //获取当前登录信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        //获取当前用户状态
        if (user.getState() == 1) {
            return Result.failed(ResultCode.FAILED_USER_BANNED);
        }
        //校验是不是发给自己
        if (user.getId() == receiveUserId) {
            return Result.failed("不能给自己发送信息");
        }

        //校验接收者不存在
        User receiveUser = userService.selectById(receiveUserId);
        if (receiveUser == null || receiveUser.getDeleteState() == 1) {
            return Result.failed("接收者异常");
        }

        //封装对象
        Message message = new Message();
        message.setPostUserId(user.getId());//发送者
        message.setContent(content); // 内容
        message.setReceiveUserId(receiveUser.getId()); //接收者

        //调用service
        messageService.create(message);
        return Result.success("发送成功");
    }

    @ApiOperation("获取未读数")
    @GetMapping("/getUnreadCount")
    public Result<Integer> getUnreadCount(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState() == 1) {
            return Result.failed(ResultCode.FAILED_USER_BANNED);
        }
        //调用service
        Integer count = messageService.selectUnreadCount(user.getId());

        return Result.success(count);
    }

    @ApiOperation("获取信息列表")
    @GetMapping("/getAll")
    public Result<List<Message>> getMessageAll(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState() == 1) {
            return Result.failed(ResultCode.FAILED_USER_BANNED);
        }

        //调用Service

        List<Message> messages = messageService.selectByReceiveUserId(user.getId());
        return Result.success(messages);
    }

    @ApiOperation("更新为已读")
    @PostMapping("/markRead")
    public Result markRead(HttpServletRequest request,
            @ApiParam("信息id") @RequestParam("id") @NonNull Long id){
        //1. 根据id查询站内信
        Message message = messageService.selectById(id);
        if(message == null || message.getDeleteState() == 1){
            return Result.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }

        //2. 判断站内信是否为自己的
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        //3. 判断接收者是不是自己
        if(user.getId() != message.getReceiveUserId()){
            //返回错误信息
            return Result.failed(ResultCode.FAILED_FORBIDDEN);
        }

        //调用service,更新为已读
        messageService.updateStatus(id, (byte) 1);

        return Result.success();
    }


}
